﻿package com.calefk.util {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	import flash.events.MouseEvent;
	import flash.system.System;
	import flash.display.BlendMode;
	import flash.ui.Keyboard;
	
	public class Logger {
		
		private static var sprite:Sprite;
		private static var canvas:MovieClip;
		private static var header:MovieClip;
		
		private static var textField:TextField;
		private static var textFormat:TextFormat;
		
		private static var line:int = 0;
		
		private static var toActiveLog:Boolean;
		private static var toActiveTrace:Boolean;
		
		private static var keyCode:int;
        
        private static var stringCheat:String = "";
        static private var activeEnterCheat = false;
		
		public static function initialize(_canvas:MovieClip , widthScreen:Number = 600, heightScreen:Number = 400 ,
				_activeLog:Boolean = true, _activeTrace:Boolean = true, _keyCode:int = 17):void {
			canvas = _canvas;
			
			toActiveLog  = _activeLog;
			toActiveTrace = _activeTrace;
            
            sprite = new Sprite();
            
            textField = new TextField();
            textField.selectable = true;
            textField.multiline = true;
            textField.background = true;
            textField.backgroundColor = 0x000000;
            textField.width  = widthScreen;
            textField.height = 160;
            textField.wordWrap = false;
            textField.blendMode = BlendMode.LAYER;
            textField.scrollRect = new Rectangle(0 , 0 , widthScreen , 160);
            textField.alpha = 0.8;
            // apply format
            textFormat = new TextFormat();
            textFormat.align = TextFormatAlign.LEFT;
            textFormat.size = 11;
            textFormat.font = "verdana";
            textField.setTextFormat(textFormat);
            textField.x = 0;
            textField.y = 20;
            sprite.addChild(textField);
            // create header
            header = new mcLogger();
            header.btnClose.width = widthScreen;
            // add events
            header.btnClose.addEventListener(MouseEvent.CLICK , onCloseClick , false , 0 , true);
            header.btnCopy.addEventListener(MouseEvent.CLICK , onCopyClick , false , 0 , true);
            header.btnClear.addEventListener(MouseEvent.CLICK , oncleanClick , false , 0 , true);
            header.x = 0;
            header.y = 0;
            sprite.addChild(header);
            // add to scene
            sprite.x = 0;
            sprite.y = heightScreen - 180;
            canvas.addChild(sprite);
            // add key code for show logger deafult 17 (Key Control)
            keyCode = _keyCode;
            canvas.stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown , false , 0 , true);
            canvas.stage.addEventListener(KeyboardEvent.KEY_UP   , onKeyUp   , false , 0 , true);

            sprite.visible = false;
            
			// First Text
			echo(":::: Initialize Logger 1.0 :::: ");
		}
        
		private static function onKeyDown(e:KeyboardEvent):void {

		}
		private static function onKeyUp(e:KeyboardEvent):void {
                switch (stringCheat) {
                    case "":
                        if (e.keyCode == 76) {
                            stringCheat = "L";
                        } else {
                            stringCheat = "";
                        }
                        break;
                    case "L":
                        if (e.keyCode == 79) {
                            stringCheat = "LO";
                        } else {
                            stringCheat = "";
                        }
                        break;
                    case "LO":
                        if (e.keyCode == 71) {
                            stringCheat = "LOG";
                        } else {
                            stringCheat = "";
                        }
                        break;
                    case "LOG":
                        if (e.keyCode == 71) {
                            stringCheat = "LOGG";
                        }
                        else {
                            stringCheat = "";
                        }
                        break;
                    case "LOGG":
                         if (e.keyCode == 13) {
                             validateCheat();
                         } 
                         stringCheat = "";
                        break;
                }
   
		}
        
        static private function validateCheat():void {
            if (stringCheat == "LOGG") {
                if (sprite) {
                    sprite.visible = true;
                }
            }
        }
		
		public static function echo(message:String):void {
			if (toActiveLog) {
				draw("00FF00" , "echo : " + message);
			}
			if (toActiveTrace) {
				trace(":::: LOG ECHO -> " + message);
			}
		}
		
		public static function warn(message:String):void {
			if (toActiveLog) {
				draw("FFFF00" , "warn : " + message);
			}
			if (toActiveTrace) {
				trace(":::: LOG WARN -> " + message);
			}
		}
		
		public static function error(message:String):void {
			if (toActiveLog) {
				draw("FF6600" , "error : " + message);
			}
			if (toActiveTrace) {
				trace(":::: LOG ERROR -> " + message);
			}
		}
		
		private static function draw(color:String , message:String):void {
			textField.htmlText += "<font color=\'#" + color +"\'>" + line + " : " + message + "</font></br>";
			textField.setTextFormat(textFormat);
			textField.scrollV = line - 1;
			line++;
		}
		
		private static function onCloseClick(e:Event):void {
			if (sprite) {
				sprite.visible = false;
			}
		}
		
		private static function onCopyClick(e:Event):void {
			System.setClipboard(textField.text);
		}
		
		private static function oncleanClick(e:Event):void {
			textField.htmlText = "";
			line = 0;
			echo("::: clean Log ::::");
		}
		
		public static function callerShowLogger():void {
			if (sprite) {
				// sprite.visible = !sprite.visible;
			}
		}
		
		public static function clean():void {
			canvas.stage.removeEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
			canvas.stage.removeEventListener(KeyboardEvent.KEY_UP   , onKeyUp);
			sprite.removeChild(header);
			sprite.removeChild(textField);
			canvas.removeChild(sprite);
			
			header = null;
			textField = null;
			sprite = null;
			canvas = null;
		}
	}

}